package com.fr.report.js;

import com.fr.base.FCloneable;
import com.fr.base.FRContext;
import com.fr.base.core.util.TemplateUtils;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.report.io.xml.ReportXMLUtils;
import com.fr.web.Repository;
import java.util.Map;
import java.util.logging.Logger;

public abstract class ServerJavaScript extends AbstractJavaScript
  implements CallBackable
{
  public static final String XML_TAG = "ServerJS";
  public static final int TYPE_PROCESSOR = 0;
  public static final int TYPE_URL = 1;
  private int type = 0;
  private String path = null;
  private JavaScript callBack;

  public int getType()
  {
    return this.type;
  }

  public void setType(int paramInt)
  {
    this.type = paramInt;
  }

  public String getPath()
  {
    return this.path;
  }

  public void setPath(String paramString)
  {
    this.path = paramString;
  }

  public JavaScript getCallBack()
  {
    return this.callBack;
  }

  public void setCallBack(JavaScript paramJavaScript)
  {
    this.callBack = paramJavaScript;
  }

  public String createJS(Repository paramRepository)
  {
    Map localMap = paramRepository.checkoutTplContext();
    if (this.type == 0)
    {
      localMap.put("url", localMap.get("servletURL"));
      localMap.put("data", "{op : 'serverjs_',class : '" + this.path + "'}");
    }
    else
    {
      localMap.put("url", this.path);
      localMap.put("data", "{}");
    }
    localMap.put("async", "true");
    localMap.put("params", "{}");
    try
    {
      String str = TemplateUtils.render("$.ajax({url : '${url}',type : 'POST',data : $.extend(${data}, ${params}),async : ${async},complete : function(res, status) {" + ((this.callBack == null) ? "" : this.callBack.createJS(paramRepository)) + "}});", localMap);
      return str;
    }
    catch (Exception localException)
    {
      FRContext.getLogger().warning(localException.getMessage());
    }
    return null;
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    String str = null;
    if (paramXMLableReader.isAttr())
    {
      if ((str = paramXMLableReader.getAttr("type")) != null)
        setType(Integer.parseInt(str));
      if ((str = paramXMLableReader.getAttr("path")) != null)
        setPath(str);
    }
    if ((paramXMLableReader.isChildNode()) && ("JavaScript".equals(paramXMLableReader.getTagName())))
      setCallBack(ReportXMLUtils.readJavaScript(paramXMLableReader));
    super.readXML(paramXMLableReader);
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    paramXMLPrintWriter.startTAG("ServerJS").attr("type", this.type).attr("path", this.path);
    if (this.callBack != null)
      ReportXMLUtils.writeXMLable(paramXMLPrintWriter, this.callBack, "JavaScript");
    super.writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.end();
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    ServerJavaScript localServerJavaScript = (ServerJavaScript)super.clone();
    if (this.callBack != null)
      localServerJavaScript.callBack = ((JavaScript)this.callBack.clone());
    return localServerJavaScript;
  }
}